package com.zbest.zpay.acctrans.component;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.zbest.common.lang.base.Money;
import com.zbest.zpay.acctrans.common.exception.AccTransRuntimeException;
import com.zbest.zpay.acctrans.common.util.BalanceTagUtils;
import com.zbest.zpay.acctrans.dal.mapper.AccountBalanceMapper;
import com.zbest.zpay.acctrans.dal.model.AccountBalance;
import com.zbest.zpay.acctrans.enums.MainActTyp;
import com.zbest.zpay.acctrans.enums.biz.AcctransBizCode;

public class CreateMainAccountBalanceComponent {
    private static final Logger logger = LoggerFactory
            .getLogger(CreateMainAccountBalanceComponent.class);

    @Autowired
    private AccountBalanceMapper accountBalanceMapper;

    public void execute(String actTyp, String usrId,
                        String actNo, String actDat, String jrnNo) {
        logger.info("[Create Main Account " + actNo + " ]");
        if (StringUtils.isBlank(actTyp) || !MainActTyp.isDefineByCode(actTyp)) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "ACT_TYP IS NULL OR define error");
        }
        if (StringUtils.isBlank(usrId) || StringUtils.isBlank(actNo)) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "USR_IS OR ACT_NO IS NULL");
        }

        logger.info("[检查该账户余额是否存在]");
        AccountBalance qryAccountBalance = accountBalanceMapper.qryByActNo(actNo);
        if (qryAccountBalance != null) {
            logger.info("[该账户余额信息已存在]");
            return;
        }

        Money zero = new Money(0, Money.DEFAULT_CURRENCY);
        String tagCreatRet = BalanceTagUtils.createBalanceTag(actNo, zero);
        AccountBalance mainAccountBalance = new AccountBalance();
        mainAccountBalance.setActNo(actNo);
        mainAccountBalance.setActTyp(actTyp);
        mainAccountBalance.setUsrId(usrId);
        mainAccountBalance.setBalTag(tagCreatRet);
        mainAccountBalance.setUpdDat(actDat);
        mainAccountBalance.setUpdJrn(jrnNo);
        accountBalanceMapper.insert(mainAccountBalance);
        logger.info("[创建主账户余额信息成功]");

    }
}
